package com.mentaleye.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.mentaleye.biz.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONObject;
import com.mentaleye.bean.OaClassBean;
import com.mentaleye.bean.OaClassRelationBean;
import com.mentaleye.bean.StudentsBean;
import com.mentaleye.dal.model.OaClassRelationExample;
import com.mentaleye.enums.DictionaryEnums;
import com.mentaleye.util.CommonResult;


@Controller
@RequestMapping(value = "/oaclassRelation")
public class OaClassRelationController {
	
	@Autowired private DictionaryBiz dictionaryBiz;
	@Autowired private OaClassBiz oaClassBiz;
	@Autowired private TeacherBiz teacherBiz;
	@Autowired private OaClassRelationBiz oaClassRelationBiz;
	@Autowired private StudentsBiz studentsBiz;
	@Autowired private GroupPersonBiz groupPersonBiz;
	
	@RequestMapping(value = "/teacherInCome")
	public ModelAndView teacherInCome(@RequestParam(value = "oaclassId",required = false) Integer oaclassId){
		ModelAndView view = new ModelAndView("ea/oaclassrelation/teacherOaClassList");
		Map<Integer,String>   classTypeMap=dictionaryBiz.queryDictionaryByParent(DictionaryEnums.CLASS_PERSON.getCode());
		view.addObject("classTypeMap", classTypeMap);
		view.addObject("classType" ,JSONObject.toJSONString(classTypeMap));
		OaClassBean oaClassBean = new OaClassBean();
		oaClassBean.setOaclassId(oaclassId);
		OaClassBean oaclass = oaClassBiz.queryOaclassData(oaClassBean);
		view.addObject("oaclass", oaclass); 
		view.addObject("teacherlist",teacherBiz.queryTeacher());
		return view;
	}
	
	@RequestMapping(value = "/studentInCome")
	public ModelAndView studentInCome(@RequestParam(value = "oaclassId",required = false) Integer oaclassId){
		ModelAndView view = new ModelAndView("ea/oaclassrelation/studentOaClassList");
		Map<Integer,String>   classTypeMap=dictionaryBiz.queryDictionaryByParent(DictionaryEnums.CLASS_PERSON.getCode());
		view.addObject("classTypeMap", classTypeMap);
		view.addObject("classType" ,JSONObject.toJSONString(classTypeMap));
		OaClassBean oaClassBean = new OaClassBean();
		oaClassBean.setOaclassId(oaclassId);
		OaClassBean oaclass = oaClassBiz.queryOaclassData(oaClassBean);
		view.addObject("oaclass", oaclass); 
		view.addObject("studentlist",oaClassRelationBiz.queryNotClassStudent());
		return view;
	}
	
	@RequestMapping(value = "/studentOut")
	public ModelAndView studentOut(@RequestParam(value = "oaclassId") Integer oaclassId){
		ModelAndView view = new ModelAndView("ea/oaclassrelation/deleteStudentsList");
		OaClassRelationExample example = new OaClassRelationExample();
		example.createCriteria().andOaclassIdEqualTo(oaclassId);
		example.createCriteria().andPersonTypeEqualTo(23);
		List<OaClassRelationBean> oaclasslist = oaClassRelationBiz.queryOaclassRelation(example);
		List<Integer> stuids = new ArrayList<Integer>();
		if(oaclasslist != null){
			for (OaClassRelationBean oaclass : oaclasslist) {
				if(oaclass.getRelationState() != 0){
					stuids.add(oaclass.getPersonId());
				}
			}
		}
		List<StudentsBean> studentslist = studentsBiz.getListBeanByIds(stuids);
		view.addObject("studentslist", studentslist);
		OaClassBean oaClassBean = new OaClassBean();
		oaClassBean.setOaclassId(oaclassId);
		OaClassBean oaclass = oaClassBiz.queryOaclassData(oaClassBean);
		view.addObject("oaclass", oaclass); 
		return view;
	}
	
	@RequestMapping(value = "/save")
	@ResponseBody
	public String save(OaClassRelationBean oaClassRelationBean,int[] personId){
		if(personId !=null && personId.length>1){
			List<OaClassRelationBean> beanlist = new ArrayList<OaClassRelationBean>();
			List<Integer> personIdList = new ArrayList<Integer>();
			for(int i=0;i<personId.length;i++){
				OaClassRelationBean oaclass = new OaClassRelationBean();
				oaclass.setOaclassId(oaClassRelationBean.getOaclassId());
				oaclass.setPersonId(personId[i]);
				oaclass.setIncomeTime(oaClassRelationBean.getIncomeTime());
				oaclass.setPersonType(23);
				oaclass.setRelationState(oaClassRelationBean.getRelationState());
				beanlist.add(oaclass);
				personIdList.add(personId[i]);
			}
			oaClassRelationBiz.removeByPersonIdList(personIdList);
			return JSONObject.toJSONString(new CommonResult(oaClassRelationBiz.addOaClassRelationBeanList(beanlist)));
		}
		return JSONObject.toJSONString(new CommonResult(oaClassRelationBiz.addOaClassRelation(oaClassRelationBean)));
	}
	
	@RequestMapping(value = "/delete")
	@ResponseBody
	public String delete(OaClassRelationBean oaClassRelationBean,int[] personId){
		oaClassRelationBean.setRelationState(0);
		CommonResult result = new CommonResult();
		List<Integer> personIdList = new ArrayList<Integer>();
		for(int i=0;i<personId.length;i++){
			personIdList.add(personId[i]);
		}
		if(personId !=null && personId.length>1){
			result = oaClassRelationBiz.updateOaClassRelationList(oaClassRelationBean, personIdList);
		}
		result = oaClassRelationBiz.updateOaClassRelation(oaClassRelationBean);
		if(result.getIsSuccess()){
			groupPersonBiz.removeList(personIdList);
		}
		return JSONObject.toJSONString(result);

	}
}
